//Form input button press
function eventAjaxSubmit(silent) {
	var bodyEle = "";
	var fullscr = "false";
	var valid = true;
	var time = parseInt(player.getCurrentTime());
	//Get the page title
	if ($('#app-title-text').text() == "Click to add") {
		title = "";
	} else {
		title = $('#app-title-text').text();
	}
	
	//Get an array of the objects in the page body
	screenObjects = $('#app-screen-content').children();
	
	//iterate through the objects creating the HTML body text
	for (i=0 ; i<screenObjects.length ; i++) {
		thisObject = screenObjects[i];
		
		//Determine what the element is
		if ($(thisObject).attr('id') == 'app-multi-choice') {
			var code;
			
			//Get the parameters
			question = $(thisObject).find('#app-multi-question').text();
			optionA = $(thisObject).find('#app-multi-question-a').text();
			optionB = $(thisObject).find('#app-multi-question-b').text();
			optionC = $(thisObject).find('#app-multi-question-c').text();
			optionD = $(thisObject).find('#app-multi-question-d').text();
			correctLetter = $(thisObject).find('#app-correct-response').val();
			correctResponse = $(thisObject).find('#app-multi-question-' + correctLetter).text();
			points = $(thisObject).find('#app-points').val();
			
			//Validate the correct letter and points drop down
			if (points == "null") {
				valid = false;
				alert("Please ensure you have selected the amount of points to be awarded from the drop" +
						" down menu");
			}
			if (correctLetter == "null") {
				valid = false;
				alert("Please ensure you have selected a correct answer from the drop down menu");
			} 
			
			//Add data to JSON object
			bodyEle += '{' +
		           '"type": "app-multi-choice",' +
		           '"properties": {' +
		               '"question": "' + question + '",' +
		               '"optionA": "' + optionA + '",' +
		               '"optionB": "' + optionB + '",' +
		               '"optionC": "' + optionC + '",' +
		               '"optionD": "' + optionD + '",' +
		               '"correctAnswer": "' + correctResponse + '",' +
		               '"points": "' + points + '"' +
		            '}' +
		        '},';
			
			
		} else if ($(thisObject).attr('id') == 'app-home') {
			src = $('#app-home-img').attr('src');
			text = $('#app-home-text').text();
			time = -1;
			bodyEle += '{' +
					'"type": "app-home",' +
					'"properties": {' +
						'"img": "' + src + '",' +
						'"text": "' + text + '"' +
					'}' +
				'},';
			
		} else {
			objectType = $(thisObject).attr('class');
			if (objectType == "app-heading-three" || objectType == "app-paragraph") {
				text = $(thisObject).find('.app').text();
				bodyEle += '{' +
						'"type": "' + objectType + '",' +
						'"properties": {' +
							'"text": "' + text + '"' +
						'}' +
					'},';
				
			} else if (objectType == "app-image") {
				fullscr = "true";
				src = $(thisObject).find('.app').attr('src');
				bodyEle += '{' +
						'"type": "' + objectType + '",' +
						'"properties": {' +
							'"url": "' + src + '"' +
						'}' +
					'},';
				
			} else if (objectType == "app-tube-status") {
				fullscr = "true";
				bodyEle += '{' +
						'"type": "' + objectType + '",' +
						'"properties": {' +
						'}' +
					'},';
				
			} else if (objectType == "app-google-plus") {
				gURL = $(thisObject).attr('src');//accept full url of google plus page so must start https://plus.google.com/
				bodyEle += '{' +
						'"type": "' + objectType + '",' +
						'"properties": {' +
							'"url": "' + gURL + '"' +
						'}' +
					'},';
				
			} else if (objectType == "app-facebook-like") {
				likeURL = encodeURIComponent($(thisObject).find('.app').text());//needs to be encoded
				bodyEle += '{' +
						'"type": "' + objectType + '",' +
						'"properties": {' +
							'"url": "' + likeURL + '"' +
						'}' +
					'},';
				
			} else if (objectType == "app-twitter-hash") {
				tag = $(thisObject).find('.app').text(); //can't contain spaces - must be alphanumeric
				bodyEle += '{' +
						'"type": "' + objectType + '",' +
						'"properties": {' +
							'"tag": "' + tag + '"' +
						'}' +
					'},';
				
			}
		}
		
	}
	
	//Remove last character from body elements object
	bodyEle = bodyEle.slice(0, bodyEle.length-1); 
	
	json = '{' +
			'"videoId": "' + videoId + '",' +
			'"viewerId": "' + viewerId + '",' +
			'"time": ' + time + ',' +
			'"title": "' + title + '",' +
			'"youtubeVideoTitle": "' + videoTitle + '",' +
			'"youtubeVideoImg": "' + videoImg + '",' +
			'"fullscreen": ' + fullscr + ',' +
			'"bodyElements": [' +
			bodyEle +
		    ']' +
			'}';
	if (valid == true) {
		$.ajax({
			type: "POST",
			url: "/event?code=1&viewer=" + viewerId,
			data: json,
			contentType: "application/json; charset=UTF-8",
			success: function(data) {
				if (data.status == "error") {
					alert("There has been an error authenticating your request and your event has not been saved." +
							" Please sign out and try again.");
				} else {
					if (silent == false) {
						//Home event submitted - alert
						alert("Home screen saved - please create your event");
						//Continue with creating events
						$('#app-homescreen').hide();
						$('#app-elements').show();
						//Clear the app screen of elements
						$('#app-screen-content').text("");
						//Set showhome to false so the home creator is not shown until next page reload - variable in eventPlayer.js
						showHome = false;
					} else if (silent == true) {
						//Home event submitted - silent
						//Continue with creating events
						$('#app-homescreen').hide();
						$('#app-elements').show();
						//Clear the app screen of elements
						$('#app-screen-content').text("");
						//Set showhome to false so the home creator is not shown until next page reload - variable in eventPlayer.js
						showHome = false;
					} else {
						//Event has been submitted
						//Hide the controls
						$('#event-controls').hide('fast');
						//Clear the app screen of elements
						$('#app-screen-content').text("");
						//display message back to user here
						alert("Event saved, please continue.");
					}
				}
			}
		});
	}
	
	return false;
}

function deleteEvent(id) {
	$.ajax({
		type: "DELETE",
		url: "/event?key=" + id + "&code=4&viewer=" + viewerId,
		success: function() {
			$('#current-events-table').children('tbody').find('#' + id).remove();
		}
	});
}

function changeVideoStatus(to) {
	dataString = 'status=' + to + '&code=2&viewer=' + viewerId + '&videoId=' + videoId;
	$.ajax({
		type: "POST",
		url: "/event",
		data: dataString,
		success: function() {
			if (to == true) {
				//Set to enabled
				$('#video-disabled-text').hide();
				$('#create-video-bg').css('background-image', 'none');
				$('#video-enable').hide();
				$('#video-disable').show();
			} else {
				//Set to disabled
				$('#video-disabled-text').show();
				$('#create-video-bg').css('background-image', "url('/images/pink-stripe.png')");
				$('#video-enable').show();
				$('#video-disable').hide();
			}
		}
	});
}